package com.dnjn.happiness.auth.controller;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.nacos.api.common.Constants;
import com.dnjn.happiness.auth.config.HappinessConfig;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import com.dnjn.happiness.auth.form.LoginBody;
import com.dnjn.happiness.auth.form.RegisterBody;
import com.dnjn.happiness.auth.service.SysLoginService;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.JwtUtils;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.security.auth.AuthUtil;
import com.dnjn.happiness.common.security.service.TokenService;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.system.api.model.LoginUser;

import java.util.List;

/**
 * token 控制
 * 
 * @author happiness
 */
@RestController
public class TokenController
{
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private HappinessConfig config;

    @GetMapping("/validLinkedUrlApi/{host}")
    public R<Boolean> validLinkedUrlApi(@PathVariable("host") String host){
        return R.ok(config.getWhiteDomain().contains(host));
    }


    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    /**
     * 微信小程序端 工号+姓名登录
     * @param
     * @return
     */
    @PostMapping("wxEmpnoLogin")
    public AjaxResult wxEmpnoLogin(@RequestBody LoginBody loginBody) {
        Assert.notNull(loginBody.getEmpno(),"工号不能为空!");
        Assert.notNull(loginBody.getUsername(),"姓名不能为空!");
        Assert.notNull(loginBody.getPassword(),"密码不能为空!");
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        LoginUser loginUser = sysLoginService.wxEmpnoLogin(loginBody.getUsername(), loginBody.getEmpno(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, tokenService.createToken(loginUser).get("token"));
        return ajax;
    }

    @PostMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody)
    {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return R.ok();
    }
}
